﻿using AutoMapper;
using MediatR;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ProcurementManagement.Applcation.CommandHanlers.ProcureApplyCommandHandler
{
    /// <summary>
    /// 采购申请添加中介者
    /// </summary>
    public class ProcureApplyCreateCommandHandler : IRequestHandler<ProcureApplyCreateCommand, int>
    {
        private readonly lnterfaceProcureApplyRepository _procureApplyRepository;//采购申请表
        private readonly lnterfacePurchaseproductRepository _purchaseproductRepository;//采购明细表
        private readonly ISqlSugarClient db;
        private readonly IMapper _mapper;

        public ProcureApplyCreateCommandHandler(lnterfaceProcureApplyRepository procureApplyRepository, lnterfacePurchaseproductRepository purchaseproductRepository, ISqlSugarClient db, IMapper mapper)
        {
            _procureApplyRepository = procureApplyRepository;
            _purchaseproductRepository = purchaseproductRepository;
            this.db = db;
            _mapper = mapper;
        }

        public async Task<int> Handle(ProcureApplyCreateCommand request, CancellationToken cancellationToken)
        {
            var res = (await _procureApplyRepository.GetAll()).Where(x=>x.PAApplyMotif==request.dto.PAApplyMotif);
            if (res.Count() > 0)
            {
                return -1;//主题不能重复
            }
            //开启事物
            db.Ado.BeginTran();
            try
            {
                //添加采购申请表
                var procureApply = _mapper.Map<ProcureApply>(request.dto);
                procureApply.PANumbers = Guid.NewGuid().ToString().Replace("-", "");
                procureApply.PANumber = Guid.NewGuid().ToString().Replace("-", "");
                var add = await _procureApplyRepository.AddAsync(procureApply);

                //添加采购明细表
                foreach (var item in request.dto.list)
                {
                    Purchaseproduct purchaseproduct = new Purchaseproduct
                    {
                        PurchaseproductId=item.PurchaseproductId,
                        PANumber = procureApply.PANumbers,
                        ProductId=item.ProductId,
                        VendorId=item.VendorId,
                        Num=item.Num,
                        Price=item.Price,
                        SumPrice=(item.Num * item.Price),
                        Datess=item.Datess,
                        Remark=item.Remark
                    };
                    await _purchaseproductRepository.AddAsync(purchaseproduct);
                }
                //提交事物
                db.Ado.CommitTran();
                return 1;

            }
            catch (Exception)
            {
                //回滚事物
                db.Ado.RollbackTran();
                throw;
            }
        }
        



    }
}
